package com.hnkywl.sts.dao.wlgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.xsgl.Psd;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-23
 * Time: 上午10:54
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class XspsDao extends SimpleHibernateTemplate<Psd,Long> {
    public Page<Psd> findXspsList(String xsdh,String psdh,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Psd> page = new Page<Psd>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (xsdh != null && !xsdh.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" xsd.xsdh like ? ");
            param.add("%" + xsdh.trim() + "%");
        }
        if (psdh != null && !psdh.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" psdh like ? ");
            param.add("%" + psdh.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Psd where 1=1" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Psd where 1=1" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition + "order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }
}
